package cn.hellohao.controller;

import cn.hellohao.dao.ImgApiMapper;
import cn.hellohao.pojo.ImgData;
import cn.hellohao.pojo.ImgDetail;
import cn.hutool.core.img.Img;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
public class ImgApiController {
    @Autowired
    private ImgApiMapper imgApiMapper;

    @GetMapping("/api/v1/img")
    public ArrayList img(){
        ArrayList<Object> arrayList = new ArrayList<>();
        List<ImgData> imgData = imgApiMapper.selectAllImgs();
        for (ImgData imgDatum : imgData) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("id",imgDatum.getId());
            map.put("title","tc");
            map.put("name","imgname");
            map.put("img_num",imgData.size());
            map.put("img_src",imgDatum.getImgurl());
            arrayList.add(map);
        }
        return arrayList;
    }

    @GetMapping("/api/v1/img/detail")
    public ImgDetail imgDetail(@RequestParam("id") String id){
        List<ImgData> imgData = imgApiMapper.selectDetail(Integer.parseInt(id));
        ImgDetail imgDetail = new ImgDetail();
        imgDetail.setCode(0);
        imgDetail.setMsg("success");
        String imgurl[] = new String[imgData.size()];
        if(imgData.size() > 0){
            for (int i = 0; i < imgData.size();i++) {
                imgurl[i] = imgData.get(i).getImgurl();
            }
            imgDetail.setData(imgurl);
        }
        return imgDetail;
    }

    @GetMapping("/api/v1/img/detailtc")
    public HashMap<Object, Object> imgTc(@RequestParam("id") String id){
        ImgData imgData = imgApiMapper.selectTc(Integer.parseInt(id));
        if(imgData != null){
            HashMap<Object, Object> map = new HashMap<>();
            map.put("code",0);
            map.put("msg","success");
            map.put("data",imgData.getImgurl());
            return map;
        }else {
            return null;
        }
    }
}
